<template>
  <div class="followup-box inner-content">
    <div class="inner inner-content-box">
      <el-form class='search-form' :inline="true" :model="searchForm" @keyup.enter.native="getDataList(1)">
        <el-form-item>
          <el-input v-model="searchForm.dept" placeholder="科室名称" clearable style='width: 160px;'></el-input>
        </el-form-item>
        <el-form-item>
          <div class="flex-start" style="align-items: flex-start;">
            <el-button type="primary" @click="getDataList(1)" icon="el-icon-search">查询</el-button>
            <el-button @click="clearHandle()" icon="el-icon-delete-solid">清空</el-button>
          </div>
        </el-form-item>
        <br />
        <el-form-item>
          <div class="flex-start" style="align-items: flex-start;">
            <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateVisible = true">新增</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="table-box" ref="tableBox">
        <el-table :data="dataList" :height="tableHeight" border size="small" style="width: 100%;">
          <el-table-column label="序号" type="index" fixed="left" header-align="center" align="center" width="70">
          </el-table-column>
          <!-- <el-table-column
            minWidth="90"
            prop="id"
            header-align="center"
            align="center"
            label="科室ID"
          > -->
          </el-table-column>
          <el-table-column minWidth="90" prop="dept" header-align="center" align="center" label="科室名称">
          </el-table-column>
          <el-table-column fixed="right" align="center" label="操作" width="100">
            <template slot-scope="scope">
              <el-button title='编辑' icon="el-icon-edit" type="primary" circle size="small"
                @click='editItem(scope.row)'></el-button>
              <el-button title='删除' icon="el-icon-delete" type="danger" circle size="small"
                @click='delItem(scope.row)'></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex-end">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="!addOrUpdateForm.id ? '新增' : '修改'" :close-on-click-modal="false" :lock-scroll="false"
      :visible.sync="addOrUpdateVisible" :before-close="closeHandle">
      <el-form inline :model="addOrUpdateForm" :rules="addOrUpdateFormRule" ref="addOrUpdateForm"
        @keyup.enter.native="dataFormSubmit()" label-width="80px">
        <el-form-item label="科室" prop="dept">
          <el-input v-model.trim="addOrUpdateForm.dept" placeholder="请填写科室" clearable style='width: 300px'></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeHandle">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchForm: {},
      // 带随访数目
      addOrUpdateVisible: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      tableHeight: 0,
      addOrUpdateForm: {
        dept: '',
        id: ''
      },
      addOrUpdateFormRule: {
        dept: [
          {
            required: true,
            message: '科室不能为空',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created () {
    setTimeout(() => {
      this.tableHeight = this.$refs.tableBox.scrollHeight
    }, 100)
    this.getDataList()
  },
  methods: {
    // 获取科室
    getDataList (page) {
      if (page) {
        this.pageIndex = page
      }
      this.$http({
        url: '/study/dept/list',
        method: 'get',
        params: {
          page: this.pageIndex,
          limit: this.pageSize,
          dept: this.searchForm.dept
        }
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },

    clearHandle () {
      this.searchForm = {}
      this.pageIndex = 1
      this.getDataList()
    },
    closeHandle () {
      this.$refs.addOrUpdateForm.resetFields()
      setTimeout(() => {
        this.addOrUpdateVisible = false
      }, 10)
    },
    dataFormSubmit () {
      this.$refs.addOrUpdateForm.validate(valid => {
        if (valid) {
          if (this.addOrUpdateForm.id) {
            // 编辑
            this.$http({
              url: '/study/dept/update',
              method: 'post',
              data: {
                id: this.addOrUpdateForm.id,
                dept: this.addOrUpdateForm.dept,
                createUserId: this.$store.state.user.id
              }
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.closeHandle()
                this.$message.success('科室编辑成功！')
                this.getDataList()
              }
            })
          } else {
            this.$http({
              url: '/study/dept/save',
              method: 'post',
              data: {
                dept: this.addOrUpdateForm.dept,
                createUserId: this.$store.state.user.id
              }
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.closeHandle()
                this.$message.success('科室保存成功！')
                this.getDataList()
              }
            })
          }
        }
      })
    },
    delItem (val) {
      const h = this.$createElement
      this.$msgbox({
        title: '消息',
        message: h('p', { style: 'display: flex;' }, [
          h('i', { class: 'el-icon-delete', style: 'font-size: 22px;color: #ff1744;font-weight: 600;margin-right: 4px' }, ' '),
          h('span', {}, `请确认是否删除科室: ${val.dept}`)
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '操作中...'
            this.$http({
              url: '/study/dept/delete',
              method: 'post',
              data: [val.id]
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message.success('科室删除成功！')
                this.getDataList()
                instance.confirmButtonLoading = false
                done()
              }
            })
          } else {
            instance.confirmButtonLoading = false
            done()
          }
        }
      })
    },
    editItem (val) {
      this.addOrUpdateForm = JSON.parse(JSON.stringify(val))
      this.addOrUpdateVisible = true
    }
  }
}
</script>
<style lang="scss">
.followup-box {
  .inner {
    .table-box {
      flex: 1;
      width: 100%;
    }
  }
}
</style>
